<template>
    <div class="default-main ba-table-box">
        <el-alert class="ba-table-alert" v-if="baTable.table.remark" :title="baTable.table.remark" type="info" show-icon />

        <!-- 表格顶部菜单 -->
        <!-- 自定义按钮请使用插槽，甚至公共搜索也可以使用具名插槽渲染，参见文档 -->
        <TableHeader
            :buttons="['refresh', 'add', 'edit', 'delete', 'comSearch', 'quickSearch', 'columnDisplay']"
            :quick-search-placeholder="t('Quick search placeholder', { fields: t('sales.salesorders.quick Search Fields') })"
        ></TableHeader>

        <!-- 表格 -->
        <!-- 表格列有多种自定义渲染方式，比如自定义组件、具名插槽等，参见文档 -->
        <!-- 要使用 el-table 组件原有的属性，直接加在 Table 标签上即可 -->
        <Table ref="tableRef"></Table>

        <!-- 表单 -->
        <PopupForm />
    </div>
</template>

<script setup lang="ts">
import { onMounted, provide, ref } from 'vue'
import { useI18n } from 'vue-i18n'
import PopupForm from './popupForm.vue'
import { baTableApi } from '/@/api/common'
import { defaultOptButtons } from '/@/components/table'
import TableHeader from '/@/components/table/header/index.vue'
import Table from '/@/components/table/index.vue'
import baTableClass from '/@/utils/baTable'

defineOptions({
    name: 'sales/salesorders',
})

const { t } = useI18n()
const tableRef = ref()
const optButtons: OptButton[] = defaultOptButtons(['edit', 'delete'])

/**
 * baTable 内包含了表格的所有数据且数据具备响应性，然后通过 provide 注入给了后代组件
 */
const baTable = new baTableClass(
    new baTableApi('/admin/sales.Salesorders/'),
    {
        pk: 'orderno',
        column: [
            { type: 'selection', align: 'center', operator: false },
            { label: t('sales.salesorders.orderno'), prop: 'orderno', align: 'center', operator: 'RANGE', sortable: false },
            { label: t('sales.salesorders.debtorno'), prop: 'debtorno', align: 'center', operatorPlaceholder: t('Fuzzy query'), operator: 'LIKE' },
            { label: t('sales.salesorders.debtornotable__name'), prop: 'debtornoTable.name', align: 'center', operatorPlaceholder: t('Fuzzy query'), render: 'tags', operator: 'LIKE' },
            { label: t('sales.salesorders.branchcode'), prop: 'branchcode', align: 'center', operatorPlaceholder: t('Fuzzy query'), operator: 'LIKE' },
            { label: t('sales.salesorders.branchcodetable__brname'), prop: 'branchcodeTable.brname', align: 'center', operatorPlaceholder: t('Fuzzy query'), render: 'tags', operator: 'LIKE' },
            { label: t('sales.salesorders.customerref'), prop: 'customerref', align: 'center', operatorPlaceholder: t('Fuzzy query'), operator: 'LIKE', sortable: false },
            { label: t('sales.salesorders.buyername'), prop: 'buyername', align: 'center', operatorPlaceholder: t('Fuzzy query'), operator: 'LIKE', sortable: false },
            { label: t('sales.salesorders.comments'), prop: 'comments', align: 'center', operatorPlaceholder: t('Fuzzy query'), operator: 'LIKE', sortable: false },
            { label: t('sales.salesorders.orddate'), prop: 'orddate', align: 'center', operator: 'eq', sortable: 'custom', width: 160 },
            { label: t('sales.salesorders.ordertype'), prop: 'ordertype', align: 'center', operatorPlaceholder: t('Fuzzy query'), operator: 'LIKE', sortable: false },
            { label: t('sales.salesorders.shipvia'), prop: 'shipvia', align: 'center', operator: 'RANGE' },
            { label: t('sales.salesorders.shipviatable__shippername'), prop: 'shipviaTable.shippername', align: 'center', operatorPlaceholder: t('Fuzzy query'), render: 'tags', operator: 'LIKE' },
            { label: t('sales.salesorders.deladd1'), prop: 'deladd1', align: 'center', operatorPlaceholder: t('Fuzzy query'), operator: 'LIKE', sortable: false },
            { label: t('sales.salesorders.deladd2'), prop: 'deladd2', align: 'center', operatorPlaceholder: t('Fuzzy query'), operator: 'LIKE', sortable: false },
            { label: t('sales.salesorders.deladd3'), prop: 'deladd3', align: 'center', operatorPlaceholder: t('Fuzzy query'), operator: 'LIKE', sortable: false },
            { label: t('sales.salesorders.deladd4'), prop: 'deladd4', align: 'center', operatorPlaceholder: t('Fuzzy query'), operator: 'LIKE', sortable: false },
            { label: t('sales.salesorders.deladd5'), prop: 'deladd5', align: 'center', operatorPlaceholder: t('Fuzzy query'), operator: 'LIKE', sortable: false },
            { label: t('sales.salesorders.deladd6'), prop: 'deladd6', align: 'center', operatorPlaceholder: t('Fuzzy query'), operator: 'LIKE', sortable: false },
            { label: t('sales.salesorders.contactphone'), prop: 'contactphone', align: 'center', operatorPlaceholder: t('Fuzzy query'), operator: 'LIKE', sortable: false },
            { label: t('sales.salesorders.contactemail'), prop: 'contactemail', align: 'center', operatorPlaceholder: t('Fuzzy query'), operator: 'LIKE', sortable: false },
            { label: t('sales.salesorders.deliverto'), prop: 'deliverto', align: 'center', operatorPlaceholder: t('Fuzzy query'), operator: 'LIKE', sortable: false },
            { label: t('sales.salesorders.deliverblind'), prop: 'deliverblind', align: 'center', operator: 'RANGE', sortable: false },
            { label: t('sales.salesorders.freightcost'), prop: 'freightcost', align: 'center', operator: 'RANGE', sortable: false },
            { label: t('sales.salesorders.fromstkloc'), prop: 'fromstkloc', align: 'center', operatorPlaceholder: t('Fuzzy query'), operator: 'LIKE' },
            { label: t('sales.salesorders.fromstkloctable__locationname'), prop: 'fromstklocTable.locationname', align: 'center', operatorPlaceholder: t('Fuzzy query'), render: 'tags', operator: 'LIKE' },
            { label: t('sales.salesorders.deliverydate'), prop: 'deliverydate', align: 'center', operator: 'eq', sortable: 'custom', width: 160 },
            { label: t('sales.salesorders.confirmeddate'), prop: 'confirmeddate', align: 'center', operator: 'eq', sortable: 'custom', width: 160 },
            { label: t('sales.salesorders.printedpackingslip'), prop: 'printedpackingslip', align: 'center', operator: 'RANGE', sortable: false },
            { label: t('sales.salesorders.datepackingslipprinted'), prop: 'datepackingslipprinted', align: 'center', operator: 'eq', sortable: 'custom', width: 160 },
            { label: t('sales.salesorders.quotation'), prop: 'quotation', align: 'center', operator: 'RANGE', sortable: false },
            { label: t('sales.salesorders.quotedate'), prop: 'quotedate', align: 'center', operator: 'eq', sortable: 'custom', width: 160 },
            { label: t('sales.salesorders.poplaced'), prop: 'poplaced', align: 'center', operator: 'RANGE', sortable: false },
            { label: t('sales.salesorders.salesperson'), prop: 'salesperson', align: 'center', operatorPlaceholder: t('Fuzzy query'), operator: 'LIKE' },
            { label: t('sales.salesorders.salespersontable__salesmanname'), prop: 'salespersonTable.salesmanname', align: 'center', operatorPlaceholder: t('Fuzzy query'), render: 'tags', operator: 'LIKE' },
            { label: t('sales.salesorders.internalcomment'), prop: 'internalcomment', align: 'center', operatorPlaceholder: t('Fuzzy query'), operator: 'LIKE', sortable: false },
            { label: t('Operate'), align: 'center', width: 100, render: 'buttons', buttons: optButtons, operator: false },
        ],
        dblClickNotEditColumn: [undefined],
        defaultOrder: { prop: 'orderno', order: 'desc' },
    },
    {
        defaultItems: { orderno: 0, buyername: null, comments: null, orddate: '1000-01-01 00:00:00', deladd4: null, contactphone: null, contactemail: null, deliverblind: 1, freightcost: 0, deliverydate: '1000-01-01 00:00:00', confirmeddate: '1000-01-01 00:00:00', printedpackingslip: 0, datepackingslipprinted: '1000-01-01 00:00:00', quotation: 0, quotedate: '1000-01-01 00:00:00', poplaced: 0, internalcomment: null },
    }
)

provide('baTable', baTable)

onMounted(() => {
    baTable.table.ref = tableRef.value
    baTable.mount()
    baTable.getIndex()?.then(() => {
        baTable.initSort()
        baTable.dragSort()
    })
})
</script>

<style scoped lang="scss"></style>
